package com.woniu.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.user.controller.form.ModifyArticle;
import com.woniu.user.controller.form.PostArticleForm;
import com.woniu.user.dao.AccountDao;
import com.woniu.user.dao.VenueDao;
import com.woniu.user.exception.WebException;
import com.woniu.user.exception.WebExceptionCode;
import com.woniu.user.model.Account;
import com.woniu.user.model.Article;
import com.woniu.user.dao.ArticleDao;
import com.woniu.user.model.Venue;
import com.woniu.user.service.ArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.user.util.BeanCopyUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 文章表 服务实现类
 * </p>
 *
 * @author Silas
 * @since 2023-02-15
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleDao, Article> implements ArticleService {
    @Resource
    private ArticleDao articleDao;
    @Resource
    private VenueDao venueDao;
    @Resource
    private AccountDao accountDao;



    @Override
    public void post(PostArticleForm postArticleForm, Integer venueId) {
        Article article = BeanCopyUtil.copyObject(postArticleForm, Article.class);
        article.setPublishTime(LocalDateTime.now());


            article.setPosterId(venueId);
            Venue venue = venueDao.selectById(venueId);
            article.setPoster(venue.getName());
            save(article);


    }

    @Override
    public Page<Article> query(String condition, Integer current, Integer size) {
        Page<Article> page = new Page<>(current == null ? 1 : current, size == null ? 4 : size);
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        if (!"".equals(condition) || condition != null) {
            queryWrapper.like("title", condition)
                    .or().eq("poster", condition)
                    .or().like("content", condition);

        }
        Page<Article> articlePage = articleDao.selectPage(page, queryWrapper);
        return articlePage;
    }

    @Override
    public void articleDelete(String userType, Integer userId, Integer id) {
        Article article = getById(id);
        if (userType.equals("场馆")) {
            Venue venue = venueDao.selectById(userId);
            if (venue.getName().equals(article.getPoster())) {
                removeById(id);
            } else {
                throw new WebException(WebExceptionCode.YOU_DO_NOT_HAVE_THIS_POWER);
            }
        } else {
            Account account = accountDao.selectById(userId);
            if (account.getNickname().equals(article.getPoster())) {
                removeById(id);
            } else {
                throw new WebException(WebExceptionCode.YOU_DO_NOT_HAVE_THIS_POWER);
            }
        }
    }

    @Override
    public void modify(String userType, Integer userId, ModifyArticle modifyArticle) {
        Article article = getById(modifyArticle.getId());
        if (userType.equals("场馆")) {
            Venue venue = venueDao.selectById(userId);
            if (venue.getName().equals(article.getPoster())) {
                article.setTitle(modifyArticle.getTitle());
                article.setContent(modifyArticle.getContent());
                updateById(article);
            } else {
                throw new WebException(WebExceptionCode.YOU_DO_NOT_HAVE_THIS_POWER);
            }
        } else {
            Account account = accountDao.selectById(userId);
            if (account.getNickname().equals(article.getPoster())) {
                article.setTitle(modifyArticle.getTitle());
                article.setContent(modifyArticle.getContent());
                updateById(article);
            } else {
                throw new WebException(WebExceptionCode.YOU_DO_NOT_HAVE_THIS_POWER);
            }
        }
    }
}
